package com.delu.controller;

import com.delu.data.Result;
import com.delu.dto.UsersDTO;
import com.delu.dto.VipDTO;
import com.delu.em.ResultEnum;
import com.delu.entity.*;
import com.delu.feign.OrderFegin;
import com.delu.feign.UserFeign;
import com.delu.mapper.IntegralExperienceMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import java.math.BigDecimal;

/**
 * <p>
 * 积分成长值表 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-23
 */
@Controller
@RequestMapping("/integral-experience")
public class IntegralExperienceController {

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private OrderFegin orderFeign;

    @Autowired
    private IntegralExperienceMapper integralExperienceMapper;

    @PostMapping("addIntegral")
    @ApiOperation("积分经验增加方法方法")
    //paramType:body 表示@RequestBody接受的实体类 path:表示路径/{} 传参  query @RequestParam接收参数
    @ApiImplicitParam(name = "integralExperience",value = "积分经验实体类",required = true,paramType = "body")
    public Result insertCoupon(@RequestParam IntegralExperience integralExperience) {
        try {
            UsersDTO usersDTO = new UsersDTO();
            //先给用户增加成长值  先查积分表是否有用户id 如果有就获取成长值进行累加 如果没有就set传过来的值
            IntegralExperience integralExperience1 = integralExperienceMapper.selectById(integralExperience.getUserId());
            Result result = userFeign.selectUserByVipId(integralExperience.getUserId());
            User user = (User)result.getData();
            if(integralExperience1 != null && !"".equals(integralExperience1)){
                //如果不是空的  进行成长值累加
                BigDecimal decimal = new BigDecimal(integralExperience1.getExperience()).add(new BigDecimal(integralExperience.getExperience()));
                long value = decimal.longValue();
                integralExperience1.setExperience(value);
                //根据会员等级查询需要升级的经验
                Result result1 = orderFeign.selectVipId(user.getVipId());
                VipDTO vip = (VipDTO)result1.getData();
                //判断用户是否需要升级
                if(vip.getScore() <= value){
                    //给用户升级  然后增加相应的倍速积分
                    user.setVipId(user.getVipId() + 1L);
                    BeanUtils.copyProperties(user,usersDTO);
                    userFeign.updateUserById(usersDTO);
                    //增加积分 原来的积分+现在传过来的积分*倍速
                    Result<VipDTO> vipResult = orderFeign.selectVipId(user.getVipId() + 1L);
                    VipDTO data = vipResult.getData();
                    BigDecimal multiply = new BigDecimal(integralExperience.getIntegral()).multiply(data.getSpeed());
                    double floor = Math.floor(multiply.longValue());
                    long l = new Double(floor).longValue();
                    integralExperience1.setIntegral(l + value);
                    //修改
                    integralExperienceMapper.updateById(integralExperience1);
                    return Result.response(ResultEnum.SUCCESS).setData("ok");
                }else{
                    Result<VipDTO> vipResult = orderFeign.selectVipId(user.getVipId());
                    VipDTO data = vipResult.getData();
                    BigDecimal multiply = new BigDecimal(integralExperience.getIntegral()).multiply(data.getSpeed());
                    double floor = Math.floor(multiply.longValue());
                    long l = new Double(floor).longValue();
                    integralExperience1.setIntegral(l + value);
                    //修改
                    integralExperienceMapper.updateById(integralExperience1);
                    return Result.response(ResultEnum.SUCCESS).setData("ok");
                }

            }
            //新增
            integralExperience1.setUserId(integralExperience.getUserId());
            Long v = integralExperience.getExperience();
            integralExperience1.setExperience(v);
            //判断第一次成长值用户是否需要升级
            Result result1 = orderFeign.selectVipId(user.getVipId());
            VipDTO vip = (VipDTO) result1.getData();
            //判断用户是否需要升级
            if(vip.getScore() <= v){
                //给用户升级  然后增加相应的倍速积分
                user.setVipId(user.getVipId() + 1L);
                BeanUtils.copyProperties(user,usersDTO);
                userFeign.updateUserById(usersDTO);
                //增加积分 积分*倍速
                Result<VipDTO> vipResult = orderFeign.selectVipId(user.getVipId() + 1L);
                VipDTO data = vipResult.getData();
                BigDecimal multiply = new BigDecimal(integralExperience.getIntegral()).multiply(data.getSpeed());
                double floor = Math.floor(multiply.longValue());
                long l = new Double(floor).longValue();
                integralExperience1.setIntegral(l);
                //修改
                integralExperienceMapper.updateById(integralExperience1);
                return Result.response(ResultEnum.SUCCESS).setData("ok");
            }else{
                Result<VipDTO> vipResult = orderFeign.selectVipId(user.getVipId());
                VipDTO data = vipResult.getData();
                BigDecimal multiply = new BigDecimal(integralExperience.getIntegral()).multiply(data.getSpeed());
                double floor = Math.floor(multiply.longValue());
                long l = new Double(floor).longValue();
                integralExperience1.setIntegral(l);
                //修改
                integralExperienceMapper.updateById(integralExperience1);
                return Result.response(ResultEnum.SUCCESS).setData("ok");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }
}
